<?php
/**************************
 * Created on Jan 22, 2011
 * By: Jos Pape
 * info: jospape@hotmail.com
 */
include_once "../../common.php";
include_once $root."/header.php";


if(isset($_POST['action']) && $_POST['action'] == "groeptoevoegen")
{
	// add user, check if all variabeles are set.
	if(!isset($_POST['Name']) || empty($_POST['Name']))
		$error['Naam'] = 'mag niet leeg zijn';
	if(!isset($error))
	{
		$query = "INSERT INTO UserGroups (
							Name,
							Description
						) VALUES (
							'".str_replace("'","",$_POST['Name'])."',	 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Description'])."'
						)";	
		$_MySQL->runQuery($query);	
	}
}
elseif(isset($_POST['action']) && $_POST['action'] == 'toevoegen')
{
	// add user, check if all variabeles are set.
	if(!isset($_POST['Firstname']) || empty($_POST['Firstname']))
		$error['Voornaam'] = 'mag niet leeg zijn';
	if(!isset($_POST['Lastname']) || empty($_POST['Lastname']))
		$error['Achternaam'] = 'mag niet leeg zijn';
	if(!isset($_POST['Email']) || empty($_POST['Email']))
		$error['E-mail'] = 'mag niet leeg zijn';
	if(!isset($_POST['Password']) || empty($_POST['Password']))
		$error['Wachtwoord'] = 'mag niet leeg zijn';
	if(!isset($_POST['GroupID']) || empty($_POST['GroupID']) || !is_numeric($_POST['GroupID']))
		$error['Groep'] = 'moet geselecteerd worden';
		
	if(!isset($error))
	{
		$query = "INSERT INTO Users (
							GroupID,			 	 	 	 	 	 	
							Username,			 	 	 	 	 	 	 
							Password,			 	 	 	 	 	 	 
							Firstname,			 	 	 	 	 	 	 
							Insertion,			 	 	 	 	 	 	 
							Lastname,			 	 	 	 	 	 	 
							Email,			 	 	 				 
							Mobile,			 	 	 	 	 	 	 
							Phonenumber
						) VALUES (
							'".str_replace("'","",$_POST['GroupID'])."',
							'".str_replace("'","",$_POST['Username'])."',			 	 	 	 	 	 	 
							MD5('".str_replace("'","",$_POST['Password'])."'),	 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Firstname'])."',		 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Insertion'])."',	 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Lastname'])."', 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Email'])."',	 	 	 				 
							'".str_replace("'","",$_POST['Mobile'])."',	 	 	 	 	 	 	 
							'".str_replace("'","",$_POST['Phonenumber'])."'
						)";	
		$_MySQL->runQuery($query);
	}
}

?>
<h1>Gebruikers beheer</h1>
<?php
if(isset($error) && is_array($error))
{
	?>
	<ul class="error">
	<?php
	foreach($error AS $what => $why)
	{
		?>
		<li><b><?=$what;?></b>: <?=$why;?></li>	
		<?php	
	}
	?>
	</ul>
	<?php
}
?>
<input type="button" value=" Gebruiker toevoegen " onClick="javascript: $('#gebruikertoevoegen').toggle(true);" />
<div id="gebruikertoevoegen" style="display:none">
	<form action="?" method="post">
		<table>
			<tr>
				<td>
					Voornaam
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Firstname" />
				</td>
			</tr>
			<tr>
				<td>
					Tussenvoegsel
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Insertion" />
				</td>
			</tr>
			<tr>
				<td>
					Achternaam
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Lastname" />
				</td>
			</tr>
			<tr>
				<td>
					E-mail
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Email" />
				</td>
			</tr>
			<tr>
				<td>
					Mobiel
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Mobile" />
				</td>
			</tr>
			<tr>
				<td>
					Telefoonnummer
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Phonenumber" />
				</td>
			</tr>
			<tr>
				<td>
					Wachtwoord
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Password" />
				</td>
			</tr>
			<tr>
				<td>
					Groep
				</td>
				<td>:</td>
				<td>
					<select name="GroupID">
						<option value="">Selecteer een groep</option>
						<?php
						$query = "SELECT * FROM UserGroups ORDER BY Name";
						$result = $_MySQL->runQuery($query);
						while($row = mysql_fetch_assoc($result))
						{
							?>
							<option value="<?=$row['ID'];?>"><?=$row['Name'];?></option>
							<?php
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="3" align="right">
					<input type="submit" value=" Aanmaken " />
					<input type="hidden" name="action" value="toevoegen" />
				</td>
			</tr>
		</table>
	</form>
</div>
<table>
	<thead>
		<tr>
			<th>
				Naam
			</th>
			<th>
				Gebruikersnaam
			</th>
			<th>
				E-mail
			</th>
			<th>
				Mobiel
			</th>
			<th>
				Telefoonnummer
			</th>
			<th>
				Groep
			</th>
			<th>
			</th>
		</tr>
	</thead>
	<tbody>
	<?php
	$query = "	SELECT 
					Users.*,
					UserGroups.Name AS GroupName 
				FROM Users 
					LEFT JOIN UserGroups ON UserGroups.ID = Users.GroupID
				ORDER BY Users.Firstname, Users.Lastname";
	$result = $_MySQL->runQuery($query);
	while($row = mysql_fetch_assoc($result))
	{
		?>
		<tr>
			<td>
				<?=$row['Firstname']." ".$row['Lastname'];?>
			</td>
			<td>
				<?=$row['Username'];?>
			</td>
			<td>
				<?=$row['Email'];?>
			</td>
			<td>
				<?=$row['Mobile'];?>
			</td>
			<td>
				<?=$row['Phonenumber'];?>
			</td>
			<td>
				<?=$row['GroupName'];?>
			</td>
			<td>
				<input type="button" value=" Bewerken " />
				<input type="button" value=" Verwijderen " />
				<input type="button" value=" Wachtword reset" /> 
			</td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>
<br />
<br />
<h1>Groeps beheer</h1>
<input type="button" value=" Groep toevoegen " onClick="javascript: $('#groeptoevoegen').toggle(true);" />
<div id="groeptoevoegen" style="display:none">
	<form action="?" method="post">
		<table>
			<tr>
				<td>
					Naam
				</td>
				<td>:</td>
				<td>
					<input type="text" name="Name" />
				</td>
			</tr>
			<tr>
				<td valign="top">
					Omschrijving
				</td>
				<td valign="top">:</td>
				<td valign="top">
					<textarea name="Description" style="width:150px; height:150px;"></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="3" align="right">
					<input type="submit" value=" Aanmaken " />
					<input type="hidden" name="action" value="groeptoevoegen" />
				</td>
			</tr>
		</table>
	</form>
</div>
<table>
	<thead>
		<tr>
			<th>
				Naam
			</th>
			<th>
				Beschrijving
			</th>
			<th>
			</th>
		</tr>
	</thead>
	<tbody>
	<?php
	$query = "	SELECT 
					* 
				FROM UserGroups 
				ORDER BY Name";
	$result = $_MySQL->runQuery($query);
	while($row = mysql_fetch_assoc($result))
	{
		?>
		<tr>
			<td>
				<?=$row['Name'];?>
			</td>
			<td>
				<?=$row['Description'];?>
			</td>
			<td>
				<input type="button" value=" Rechten " />
			</td>
		</tr>
		<?php
	}
	?>
	</tbody>
</table>
<?php
include_once $root."/footer.php";
?>
